import Vue from 'vue'
import VueRouter from 'vue-router'
import { getUserAuthority } from '@/unit/auth'
import { filterRouter } from '@/router/activityRouter'

// 解决 vue-router重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

import store from '@/store'

Vue.use(VueRouter)

// 常量路由
export const constantRohtes = [
	{
		path: '/login',
		component: reslove => require(['@/pages/Login'], reslove),
		name: 'Login',
		hidden: true,
		meta: {
			auth: false
		}
	},
	{
		path: '*',
		redirect: '/404',
		hidden: true,
		meta: {
			auth: false
		}
	},
	{
		path: '/404',
		component: reslove => require(['@/pages/404'], reslove),
		hidden: true,
		meta: {
			auth: false
		}
	},
	{
		path: '/',
		component: reslove => require(['@/Layout'], reslove),
		name: 'home',
		// redirect: '/dashboard',
		meta: {
			auth: true,
		},
		children: [
			{
				path: 'dashboard',
				name: 'Dashboard',
				component: reslove => require(['@/pages/Dashboard'], reslove),
				meta: {
					title: '仪表盘',
					icon: 'dashboard',
					auth: true
				}
			},
			{
				path: 'user',
				name: 'User',
				component: reslove => require(['@/pages/User'], reslove),
				redirect: '/user/organization',
				meta: {
					title: '员工管理',
					icon: 'user',
					father: 'User',
					auth: true
				},
				children: [
					{
						path: 'organization',
						name: 'Organization',
						component: reslove => require(['@/pages/User/Organization'], reslove),
						meta: {
							title: '组织架构',
							itemValue: 'Organization',
							father: 'User',
							auth: true
						},
						children: [
							{
								path: 'userinfo',
								name: 'UserInfo',
								component: reslove => require(['@/pages/User/Organization/UserInfo'], reslove),
								meta: {
									title: '员工信息',
									itemValue: 'Organization',
									father: 'User',
								}
							},
							{
								path: 'addUser',
								name: 'AddUser',
								component: reslove => require(['@/pages/User/Organization/AddUser'], reslove),
								meta: {
									title: '添加员工',
									itemValue: 'Organization',
									father: 'User'
								}
							},
							{
								path: 'divisional',
								name: 'Divisional',
								component: reslove => require(['@/pages/User/Organization/Divisional'], reslove),
								meta: {
									title: '部门管理',
									itemValue: 'Organization',
									father: 'User'
								}
							},
							{
								path: 'position',
								name: 'Position',
								component: reslove => require(['@/pages/User/Organization/Position'], reslove),
								meta: {
									title: '岗位管理',
									itemValue: 'Organization',
									father: 'User'
								}
							},
							{
								path: 'salary',
								name: 'Salary',
								component: reslove => require(['@/pages/User/Organization/Salary'], reslove),
								meta: {
									title: '工资结算',
									itemValue: 'Organization',
									father: 'User'
								}
							}
						]
					},
					{
						path: 'authority',
						name: 'Authority',
						component: reslove => require(['@/pages/User/Authority'], reslove),
						meta: {
							title: '权限管理',
							itemValue: 'Authority',
							father: 'User',
							auth: true
						}
					},
					{
						path: 'userspace',
						name: 'UserSpace',
						component: reslove => require(['@/pages/UserSpace'], reslove),
						redirect: '/user/userspace/myuserinfo',
						meta: {
							title: '个人空间',
							itemValue: 'UserSpace',
							father: 'User',
							auth: true
						},
						children: [
							{
								path: 'myuserinfo',
								name: 'MyUserInfo',
								component: reslove => require(['@/pages/UserSpace/MyUserInfo'], reslove),
								meta: {
									title: '我的信息',
									itemValue: 'UserSpace',
									father: 'User'
								}
							},{
								path: 'myapprove',
								name: 'MyApprove',
								component: reslove => require(['@/pages/UserSpace/MyApprove'], reslove),
								meta: {
									title: '我的审批',
									itemValue: 'UserSpace',
									father: 'User'
								}
							}
						]
					},
				]
			},
			{
				path: 'attendance',
				name: 'Attendance',
				component: reslove => require(['@/pages/Attendance'], reslove),
				redirect: '/attendance/AttendanceSheet',
				meta: {
					title: '考勤管理',
					icon: 'calendar',
					father: 'Attendance',
					auth: true
				},
				children: [
					{
						path: 'attendancesheet',
						name: 'AttendanceSheet',
						component: reslove => require(['@/pages/Attendance/AttendanceSheet'], reslove),
						meta: {
							title: '考勤表',
							father: 'Attendance',
							itemValue: 'AttendanceSheet',
							auth: true
						}
					},
					{
						path: 'leavework',
						name: 'LeaveWork',
						component: reslove => require(['@/pages/Attendance/LeaveWork'], reslove),
						meta: {
							title: '调休加班',
							father: 'Attendance',
							itemValue: 'LeaveWork',
							auth: true
						}
					},
					{
						path: 'setting',
						name: 'Setting',
						component: reslove => require(['@/pages/Attendance/Setting'], reslove),
						meta: {
							title: '考勤设置',
							father: 'Attendance',
							itemValue:'Setting',
							auth: true
						}
					}
				]
			},
			{
				path: 'project',
				name: 'Project',
				component: reslove => require(['@/pages/Project'], reslove),
				redirect: '/project/progress',
				meta: {
					title: '项目管理',
					icon: 'server',
					father: 'Project',
					auth: true
				},
				children: [
					{
						path: 'progress',
						name: 'Progress',
						component: reslove => require(['@/pages/Project/Progress'], reslove),
						meta: {
							title: '项目进度',
							father: 'Project',
							itemValue: 'Progress',
							auth: true
						}
					},
					{
						path: 'projectList',
						name: 'ProjectList',
						component: reslove => require(['@/pages/Project/ProjectList'], reslove),
						meta: {
							title: '项目列表',
							father: 'Project',
							itemValue: 'ProjectList',
							auth: true
						},
						children: [
							{
								path: 'addproject',
								name: 'AddProject',
								component: reslove => require(['@/pages/Project/ProjectList/AddProject'], reslove),
								meta: {
									title: '添加项目',
									itemValue: 'ProjectList',
									father: 'Project',
								}
							},{
								path: 'projectinfo',
								name: 'ProjectInfo',
								component: reslove => require(['@/pages/Project/ProjectList/ProjectInfo'], reslove),
								meta: {
									title: '项目详情',
									itemValue: 'ProjectList',
									father: 'Project',
								}
							},{
								path: 'updateproject',
								name: 'UpdateProject',
								component: reslove => require(['@/pages/Project/ProjectList/UpdateProject'], reslove),
								meta: {
									title: '编辑项目',
									itemValue: 'ProjectList',
									father: 'Project',
								}
							}
						]
					}
				]
			},
			{
				path: 'suggestion',
				name: 'Suggestion',
				component: reslove => require(['@/pages/Suggestion'], reslove),
				redirect: '/suggestion/vote',
				meta: {
					title: '投票留言',
					icon: 'chat',
					father: 'Suggestion',
					auth: true
				},
				children: [
					{
						path: 'vote',
						name: 'Vote',
						component: reslove => require(['@/pages/Suggestion/Vote'], reslove),
						meta: {
							title: '投票',
							father: 'Suggestion',
							itemValue: 'Vote',
							auth: true
						}
					},
					{
						path: 'message',
						name: 'Message',
						component: reslove => require(['@/pages/Suggestion/Message'], reslove),
						meta: {
							title: '留言',
							father: 'Suggestion',
							itemValue: 'Message',
							auth: true
						}
					}
				]
			}
		]
	}
]



// 所有路由
const createRouter = () => new VueRouter({
	scrollBehavior: () => ({ y: 0 }),
	routes: constantRohtes
})

// 创建路由实例
const router = createRouter()

// 前置路由守卫
router.beforeEach((to, from, next) => {
	if (to.meta.auth === false){
		next()
		return
	}
	// 本地登录校验
	if (!store.state.user.token){
		router.push({
			name: 'Login'
		})
		return
	}
	if(to.path === '/'){
		let arr = filterRouter(router.options.routes)
		router.push({
			name: arr[3].children[0].name
		})
		return
	}
	next()
})

export default router